<template>
  <view class="container">
    <!-- 头部区域 -->
    <view class="header">
      <view class="back-button" @click="goBack">
        <text class="iconfont icon-back">←</text>
      </view>
      <view class="title">{{ categoryName }}</view>
    </view>

    <!-- 商户列表 -->
    <view class="merchant-section">
      <view class="section-header">
        <text class="section-title">{{ categoryName }}商家</text>
        <view class="sort-options">
          <text class="sort-item" :class="{ active: sortType === 'distance' }" @click="setSortType('distance')">距离</text>
          <text class="sort-item" :class="{ active: sortType === 'sales' }" @click="setSortType('sales')">销量</text>
          <text class="sort-item" :class="{ active: sortType === 'rating' }" @click="setSortType('rating')">评分</text>
        </view>
      </view>

      <view class="merchant-list">
        <view class="merchant-item" v-for="(item, index) in filteredMerchantList" :key="index"
          @click="goToMerchantDetail(item.id)">
          <image class="merchant-logo" :src="item.logo" mode="aspectFill"></image>
          <view class="merchant-info">
            <view class="merchant-name-row">
              <text class="merchant-name">{{ item.name }}</text>
              <!-- 评分暂时隐藏 -->
        <!-- <view class="merchant-rating">
          <text class="rating-value">{{ item.rating }}</text>
          <text class="rating-star">★</text>
        </view> -->
            </view>
            <view class="merchant-meta">
              <text class="meta-item">{{ estimateTime(item.baseTime, item.distance) }}</text>
              <text class="meta-divider">|</text>
              <text class="meta-item">{{ item.distance }}km</text>
              <text class="meta-divider">|</text>
              <text class="meta-item">起送¥{{ item.minPrice }}</text>
            </view>
            <view class="merchant-tags">
              <text class="tag-item" v-if="item.deliveryFee === 0">免配送费</text>
              <text class="tag-item" v-else>配送费¥{{ item.deliveryFee }}</text>
              <text class="tag-item" v-if="item.sales > 50">月售{{ item.sales }}+</text>
            </view>
          </view>
        </view>

        <view v-if="filteredMerchantList.length === 0" class="empty-tip">
          <image src="/static/icons/empty.png" mode="aspectFit" class="empty-icon"></image>
          <text class="empty-text">该分类暂无商户</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { getMerchantPage } from '@/api/merchant.js';
import estimateTime from '@/utils/time.js';
import { getCurrentLocation } from '@/utils/location.js';

const categoryId = ref(0);
const categoryName = ref('');
const sortType = ref('distance'); // 默认按距离排序

// 根据分类筛选商户列表
const merchantList = ref([]);

const loadMerchantList = async () => {
  try {
    const params = {
      categoryId: categoryId.value,
      sortType: sortType.value
    };
    
    const location = await getCurrentLocation();
    params.longitude = location.longitude;
    params.latitude = location.latitude;
    
    const res = await getMerchantPage(params);
    merchantList.value = res.data.list;
  } catch (e) {
    console.error('获取商户列表失败', e);
  }
};

const filteredMerchantList = computed(() => {
  return merchantList.value;
});

onLoad((options) => {
  console.log("onLoad options===", options);

  if (options.id) {
    categoryId.value = parseInt(options.id);
    categoryName.value = options.name;
    loadMerchantList();
  }
});

// 返回上一页
const goBack = () => {
  uni.navigateBack();
};

// 设置排序方式
const setSortType = (type) => {
  sortType.value = type;
  loadMerchantList();
};

// 跳转到商户详情页
const goToMerchantDetail = (merchantId) => {
  uni.navigateTo({
    url: `/pages/merchant/detail?id=${merchantId}`
  });
};
</script>

<style>
.container {
  min-height: 100vh;
  padding-bottom: 100rpx;
  background: #f0f2f5;
}

/* 头部区域样式 */
.header {
  display: flex;
  align-items: center;
  height: 90rpx;
  padding: 0 30rpx;
  background: #ffffff;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
}

.back-button {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.3s;
}

.back-button:active {
  background: rgba(0, 0, 0, 0.05);
}

.icon-back {
  font-size: 40rpx;
  background: linear-gradient(135deg, #ff6b6b, #ff8e8e);
  -webkit-background-clip: text;
  color: transparent;
}

.title {
  flex: 1;
  text-align: center;
  font-size: 32rpx;
  font-weight: bold;
  background: linear-gradient(135deg, #333, #666);
  -webkit-background-clip: text;
  color: transparent;
  margin-right: 60rpx;
}

/* 商户列表样式 */
.merchant-section {
  background: #ffffff;
  padding: 20rpx 30rpx;
  margin: 20rpx;
  border-radius: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
  padding-bottom: 20rpx;
  border-bottom: 2rpx solid rgba(0, 0, 0, 0.05);
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  background: linear-gradient(135deg, #333, #666);
  -webkit-background-clip: text;
  color: transparent;
}

.sort-options {
  display: flex;
  background: #f7f8fa;
  border-radius: 30rpx;
  padding: 4rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.sort-item {
  font-size: 26rpx;
  color: #666;
  margin-left: 10rpx;
  padding: 8rpx 20rpx;
  border-radius: 25rpx;
  transition: all 0.3s;
}

.sort-item.active {
  background: linear-gradient(135deg, #ff9a9e 0%, #ff6b6b 100%);
  color: #fff;
  font-weight: bold;
  box-shadow: 0 2rpx 10rpx rgba(255, 107, 107, 0.3);
}

.merchant-list {
  padding-bottom: 20rpx;
}

.merchant-item {
  display: flex;
  padding: 20rpx;
  margin-bottom: 20rpx;
  background: #ffffff;
  border-radius: 16rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
  border: 1rpx solid #f0f0f0;
  transition: all 0.3s;
}

.merchant-item:active {
  transform: scale(0.98);
  background: #fafafa;
}

.merchant-logo {
  width: 120rpx;
  height: 120rpx;
  border-radius: 12rpx;
  margin-right: 20rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}

.merchant-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.merchant-name-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10rpx;
}

.merchant-name {
  font-size: 30rpx;
  font-weight: bold;
  background: linear-gradient(135deg, #333, #666);
  -webkit-background-clip: text;
  color: transparent;
}

.merchant-rating {
  display: flex;
  align-items: center;
  background: linear-gradient(135deg, #ff9a9e 0%, #ff6b6b 100%);
  padding: 4rpx 12rpx;
  border-radius: 20rpx;
}

.rating-value {
  font-size: 26rpx;
  color: #fff;
  margin-right: 4rpx;
}

.rating-star {
  color: #fff;
  font-size: 24rpx;
}

.merchant-meta {
  display: flex;
  align-items: center;
  margin: 10rpx 0;
}

.meta-item {
  font-size: 24rpx;
  color: #666;
}

.meta-divider {
  margin: 0 10rpx;
  color: #ddd;
}

.merchant-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 10rpx;
}

.tag-item {
  font-size: 22rpx;
  color: #ff6b6b;
  background: #fff1f0;
  padding: 4rpx 12rpx;
  border-radius: 20rpx;
  border: 1rpx solid #ffccc7;
}

/* 空状态 */
.empty-tip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60rpx 0;
}

.empty-icon {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 20rpx;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}
</style>